<?php
require_once 'includes/config.php';
require_once 'includes/functions.php';
require_once 'includes/class.phpmailer.php';

if(isset($_GET['usn']) && isset($_GET['cnc']) && $_GET['usn']!='' && $_GET['cnc']!='')
{
 $userName=$_GET['usn'];
 $cfmCode=$_GET['cnc'];
 $sqlSelUsrTmp="SELECT * FROM tbl_temp_user WHERE username='$userName' and confirmation_code ='$cfmCode'";
 $rstSelUsrTmp=mysql_query($sqlSelUsrTmp);
 $noSelUsrTmp=mysql_num_rows($rstSelUsrTmp);
 if($noSelUsrTmp>0)
 {
  $rowSelUsrTmp=mysql_fetch_array($rstSelUsrTmp);
  extract($rowSelUsrTmp);
  //print_r($rowSelUsrTmp);
  $sqlSelUsr="SELECT email FROM tbl_users WHERE username='$userName'";
  $rstSelUsr=mysql_query($sqlSelUsr);
  $noSelUsr=mysql_num_rows($rstSelUsr);
  if($noSelUsr==0) 
  {
   $sqlInsUsr="INSERT INTO tbl_users SET   
		username='".$username."',
		password='".$password."',
		user_type=".$user_type.",
		first_name='".$first_name."',
		last_name='".$last_name."',
		address_line_1='".$address_line_1."',
		address_line_2='".$address_line_2."',
		address_line_3='".$address_line_3."',
		city='".$city."',
		state='".$state."',
		country='".$country."',
		zip='".$zip."',
		phone_number_1='".$phone_number_1."',
		email='".$email."'";
		
		
   
   if($user_type=='1' || $user_type=='2')
    $sqlInsUsr=$sqlInsUsr.", status='".$status."', secret_question='".$secret_question."',province='".$province."',secret_answer='".$secret_answer."',dob='".$dob."',created_date='".$created_date."'";
   else if($user_type=='3')
    $sqlInsUsr=$sqlInsUsr.", status=2 ,bus_id='".$bus_id."',other_business_type='".$other_business_type."',secret_question='".$secret_question."',secret_answer='".$secret_answer."',business_name='".$business_name."' ,phone_number_2='".$phone_number_2."',created_date='".$created_date."',dob='".$dob."',businessEmail='".$businessEmail."',physicalLocation='".$physicalLocation."'";
   else if($user_type=='4')
    $sqlInsUsr=$sqlInsUsr.", status=4 ,secret_question='".$secret_question."',secret_answer='".$secret_answer."',business_name='".$business_name."' ,phone_number_2='".$phone_number_2."',created_date='".$created_date."'";
   else if($user_type=='5')
    $sqlInsUsr=$sqlInsUsr.", status=2 ,secret_question='".$secret_question."',secret_answer='".$secret_answer."',business_name='".$business_name."',phone_number_2='".$phone_number_2."',special_in='".$special_in."',dob='".$dob."',created_date='".$created_date."',businessEmail='".$businessEmail."'";
//	echo $sqlInsUsr;
//	exit;
   $rstInsUsr = mysql_query($sqlInsUsr)or die(mysql_error());
   $insertedId = mysql_insert_id(); 
   //$insertedId = '149';
   $sqlSelItem="select * from tbl_referer where ref_email like '%".$email."%'";
   $rstSelItem=mysql_query($sqlSelItem);
   $rowSelItem=mysql_fetch_array($rstSelItem);	
   if(is_array($rowSelItem))
   {
    $sqlPost="update tbl_referer set refer_to_user_id='".$insertedId."',isActive='1' where ref_email like '%".$email."%'";
    
    $rstPost=mysql_query($sqlPost) or die(mysql_error());		
   }
   
   if($insertedId>0)
   {
    if($user_type==1 || $user_type==2)
    {
     $sql="select count(*) as cnt from tbl_users where status=4 AND (user_type=1 OR user_type=2) AND (first_name like '%".$first_name."%' OR last_name like '%".$last_name."%'  OR dob='".$dob."')";
     $rs=mysql_query($sql) or die(mysql_error());
     $row=mysql_fetch_array($rs);
     $cnt=$row['cnt'];
    
     if($cnt>0)
     {
     //echo "update tbl_users set status='3' where user_id=$insertedId";
      $sql=mysql_query("update tbl_users set status='3' where user_id='".$insertedId."'") or die(mysql_error());
      //mysql_query($sql) or die(mysql_error());
      
      $sqlSelUsr="SELECT email FROM tbl_users WHERE user_id='$insertedId'";
      $rstSelUsr=mysql_query($sqlSelUsr);
      $noSelUsr=mysql_num_rows($rstSelUsr);
      $rowEmailuser=mysql_fetch_array($rstSelUsr);
      $email=$rowEmailuser['email'];
      //$statusToChange=$_POST['accountStatus'];
      //echo $sqlUpdUsr = "UPDATE tbl_users SET status=".$statusToChange." WHERE user_id='$userId'";
      //$rstUpdUsr = mysql_query($sqlUpdUsr) or die(mysql_error());
      if($rstSelUsr)
      {
       //$msg = "User account status changed ."; 
       $sqlEmail="select * from tbl_emails where email_type='OnHold'";
	   $resEmail=mysql_query($sqlEmail);
	   $rowEmail=mysql_fetch_array($resEmail);
	   $mailmsg="<table width='100%' border='0' style='font-family:Verdana, Arial, Helvetica, sans-serif;font-size:12px; line-height:25px'>
	   <tr>
	     <td width='18%' height='41'><table width='100%' border='0'>
	       <tr>
		 <td width='19%' height='68'><img src='".$base_url."images/logo.png' /></td>
		 <td width='81%'> </td>
	       </tr>
	     </table></td>
	   </tr>
	   <tr>
	     <td height='32' colspan='2' style='font-family:Verdana, Arial, Helvetica, sans-serif;font-size:11px;'>".$rowEmail['email_body']."</td>
	   </tr>
	   
	  
	 </table>
	 ";
	// echo $email;
	 $mail = new PHPMailer(); // defaults to using php "mail()"
	 $mail->MsgHTML($mailmsg);
	 $mail->From       = "mail@swapen.com";
	 $mail->FromName   = "Swapen.com";
	 $mail->Subject    = $rowEmail['email_subject'];
	 $mail->AddAddress($email);
	 $mail->Send();
	 
	 $msg="<font face=verdana size=2 color='#005B00'><b>Your account is onhold will confirm soon.</b></font>";
      }
     }
     else
     {
		 $sqlEmail2="select * from tbl_emails where email_type='Confirmation'";
		 $resEmail2=mysql_query($sqlEmail2);
		 $rowEmail2=mysql_fetch_array($resEmail2);
		 $subject="Congratulations! You are now a member on Swapen.com";
		 $message=str_replace('%fname%',stripslashes($first_name),str_replace('%lname%',$last_name,$rowEmail2['email_body']));
		 $headers = "From: Swapen.com<mail@swapen.com> \r\n";
		 $headers .= 'MIME-Version: 1.0' . "\r\n";
		 $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
		 mail($email,$subject, $message, $headers);
	   //$msg = "<font face=verdana size=2 color='#FF0000'><b>Your account successfully confirmed.</b></font>";
          echo $msg="<br /><font face='verdana' size='2' color='#005B00'><b>Your account successfully confirmed, <a href='".$base_url."index.php?signin'>Signin here</a> to continue.</b></font>";
     }
    }//if($user_type==1 || $user_type==2)
    if($user_type==3)
    {
     $sqlEmail2="select * from tbl_emails where email_type='Confirmation'";
	 $resEmail2=mysql_query($sqlEmail2);
	 $rowEmail2=mysql_fetch_array($resEmail2);
	 $subject="Congratulations! You are now a member on Swapen.com";
	 $message=str_replace('%fname%',stripslashes($first_name),str_replace('%lname%',$last_name,$rowEmail2['email_body']));
	 $headers = "From: Swapen.com<mail@swapen.com> \r\n";
	 $headers .= 'MIME-Version: 1.0' . "\r\n";
	 $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
	 mail($email,$subject, $message, $headers);
	 $msg="<font face=verdana size=2 color='#005B00'><b>There is a 1-3 business day wait for verification purposes, but you can start creating your inventory list today! Once you are verified as a Swapen Merchant, your inventory list will go online!</b></font>";
    }
    else if($user_type==5)
    {
     $sqlEmail2="select * from tbl_emails where email_type='Confirmation'";
	 $resEmail2=mysql_query($sqlEmail2);
	 $rowEmail2=mysql_fetch_array($resEmail2);
	 $subject="Congratulations! You are now a member on Swapen.com";
	 $message=str_replace('%fname%',stripslashes($first_name),str_replace('%lname%',$last_name,$rowEmail2['email_body']));
	 $headers = "From: Swapen.com<mail@swapen.com> \r\n";
	 $headers .= 'MIME-Version: 1.0' . "\r\n";
	 $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
	 mail($email,$subject, $message, $headers);
     $msg="<font face=verdana size=2 color='#005B00'><b>There is a 1-3 business day wait for verification purposes, but you can start creating your inventory list today! Once you are verified as a Swapen Wholeseller, your inventory list will go online!</b></font>";
    }
    else
    {
     $sqlEmail2="select * from tbl_emails where email_type='Confirmation'";
	 $resEmail2=mysql_query($sqlEmail2);
	 $rowEmail2=mysql_fetch_array($resEmail2);
	 $subject="Congratulations! You are now a member on Swapen.com";
	 $message=str_replace('%fname%',stripslashes($first_name),str_replace('%lname%',$last_name,$rowEmail2['email_body']));
	 $headers = "From: Swapen.com<mail@swapen.com> \r\n";
	 $headers .= 'MIME-Version: 1.0' . "\r\n";
	 $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
	 mail($email,$subject, $message, $headers);
     $msg="<font face=verdana size=2 color='#005B00'><b>Your account successfully confirmed, <a href='".$base_url()."index.php?signin'>Signin here</a> to continue.</b></font>";
    }
   }//if($insertedId>0)
   else
    $msg = "<font face=verdana size=2 color='#FF0000'><b>This email id already exist in another account, enter valid one.</b></font>"; 
  }//if($noSelUsr==0)
  else
   $msg = "<font face=verdana size=2 color='#FF0000'><b>Invalid confirmation code.</b></font>";
 }
 else
  $msg = "<font face=verdana size=2 color='#FF0000'><b>Invalid URL.</b></font>";
}
  
/*
if(isset($_GET['usn']) && isset($_GET['cnc']) && $_GET['usn']!='' && $_GET['cnc']!='')
{
$userName=$_GET['usn'];
$cfmCode=$_GET['cnc'];
$sqlSelUsrTmp="SELECT * FROM tbl_temp_user WHERE username='$userName' and confirmation_code ='$cfmCode'";
$rstSelUsrTmp=mysql_query($sqlSelUsrTmp);
$noSelUsrTmp=mysql_num_rows($rstSelUsrTmp);

if($noSelUsrTmp>0)
{
 $rowSelUsrTmp=mysql_fetch_array($rstSelUsrTmp);
 extract($rowSelUsrTmp);
  $sqlSelUsr="SELECT email FROM tbl_users WHERE username='$userName'";
  $rstSelUsr=mysql_query($sqlSelUsr);
  echo $noSelUsr=mysql_num_rows($rstSelUsr);
  
  if($noSelUsr==0) 
  {
   $sqlInsUsr="INSERT INTO tbl_users SET   
  				 	username='".$username."',
					password='".$password."',
					user_type=".$user_type.",
					first_name='".$first_name."',
					last_name='".$last_name."',
					address_line_1='".$address_line_1."',
					address_line_2='".$address_line_2."',
					address_line_3='".$address_line_3."',
					city='".$city."',
					state='".$state."',
					country='".$country."',
					zip='".$zip."',
					phone_number_1='".$phone_number_1."',
					email='".$email."'";
				
					
	if($user_type=='1' || $user_type=='2')
      $sqlInsUsr=$sqlInsUsr.", status=1 , secret_question='".$secret_question."',province='".$province."',secret_answer='".$secret_answer."',dob='".$dob."',created_date='".$created_date."'";
  else if($user_type=='3')
    $sqlInsUsr=$sqlInsUsr.", status=2 ,bus_id='".$bus_id."',secret_question='".$secret_question."',secret_answer='".$secret_answer."',business_name='".$business_name."' ,phone_number_2='".$phone_number_2."',created_date='".$created_date."'";
  else if($user_type=='4')
    $sqlInsUsr=$sqlInsUsr.", status=4 ,secret_question='".$secret_question."',secret_answer='".$secret_answer."',business_name='".$business_name."' ,phone_number_2='".$phone_number_2."',created_date='".$created_date."'";
	else if($user_type=='5')
	$sqlInsUsr=$sqlInsUsr.", status=2 ,secret_question='".$secret_question."',secret_answer='".$secret_answer."',business_name='".$business_name."' ,phone_number_2='".$phone_number_2."',special_in='".$special_in."'";
	$rstInsUsr = mysql_query($sqlInsUsr)or die(mysql_error());
	$insertedId = mysql_insert_id(); 
	
	$sqlSelItem="select * from tbl_referer where ref_email like '%".$email."%'";
	$rstSelItem=mysql_query($sqlSelItem);
	$rowSelItem=mysql_fetch_array($rstSelItem);	
	if(is_array($rowSelItem))
	{
		$sqlPost="update tbl_referer set refer_to_user_id='".$insertedId."',isActive='1' where ref_email like '%".$email."%'";
		//echo $sqlPost;
		$rstPost=mysql_query($sqlPost) or die(mysql_error());		
	}
	
	if($insertedId>0)
	{
	
	
	
	if($user_type==1 || $user_type==2)
	{
	   $sql="select count(*) as cnt from tbl_users where status=4 AND (user_type=1 OR user_type=2) AND (first_name like '%".$first_name."%' OR last_name like '%".$last_name."%'  OR dob='".$dob."')";
	   $rs=mysql_query($sql);
	   $row=mysql_fetch_array($rs);
	   $cnt=$row['cnt'];
	
	  if($cnt>0)
	   {
	      $sql=mysql_query("update tbl_users set status='3' where user_id=$insertedId");
	      mysql_query($sql);
	      
	      $sqlSelUsr="SELECT email FROM tbl_users WHERE user_id='$insertedId'";
	     $rstSelUsr=mysql_query($sqlSelUsr);
	     $noSelUsr=mysql_num_rows($rstSelUsr);
	     $rowEmailuser=mysql_fetch_array($rstSelUsr);
	     $email=$rowEmailuser['email'];
	   $statusToChange=$_POST['accountStatus'];
	   $sqlUpdUsr = "UPDATE tbl_users SET status=".$statusToChange." WHERE user_id='$userId'";
	   $rstUpdUsr = mysql_query($sqlUpdUsr) or die(mysql_error());
	   if($rstUpdUsr) { $msg = "User account status changed ."; 
	   	   
	    $sqlEmail="select * from tbl_emails where email_type='OnHold'";
		 $resEmail=mysql_query($sqlEmail);
		$rowEmail=mysql_fetch_array($resEmail);
		 $mailmsg="<table width='100%' border='0' style='font-family:Verdana, Arial, Helvetica, sans-serif;font-size:12px; line-height:25px'>
		 <tr>
		   <td width='18%' height='41'><table width='100%' border='0'>
		     <tr>
		       <td width='19%' height='68'><img src='".$base_url."images/logo.png' /></td>
		       <td width='81%'> </td>
		     </tr>
		   </table></td>
		 </tr>
		 <tr>
		   <td height='32' colspan='2' style='font-family:Verdana, Arial, Helvetica, sans-serif;font-size:11px;'>".$rowEmail['email_body']."</td>
		 </tr>
		 
		
	       </table>
	       ";
		       $mail = new PHPMailer(); // defaults to using php "mail()"
		       $mail->MsgHTML($mailmsg);
		       $mail->From       = "mail@swapen.com";
		       $mail->FromName   = "Swapen.com";
		       $mail->Subject    = $rowEmail['email_subject'];
		       $mail->AddAddress($email, $first_name);
		       $mail->Send();
		       
	   }
	}
	 $sqlEmail="select * from tbl_emails where email_type='Confirmation'";
	 $resEmail=mysql_query($sqlEmail);
	 $rowEmail=mysql_fetch_array($resEmail);
	  $mailmsg="<table width='100%' border='0' style='font-family:Verdana, Arial, Helvetica, sans-serif;font-size:12px; line-height:25px'>
  <tr>
    <td width='18%' height='41'><table width='100%' border='0'>
      <tr>
        <td width='19%' height='68'><img src='".$base_url."images/logo.png' /></td>
        <td width='81%'> </td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td height='32' colspan='2' style='font-family:Verdana, Arial, Helvetica, sans-serif;font-size:11px;'>".$rowEmail['email_body']."</td>
  </tr>
  
 
</table>
";
		$mail = new PHPMailer(); // defaults to using php "mail()"
	  	$mail->MsgHTML($mailmsg);
		$mail->From       = "mail@swapen.com";
		$mail->FromName   = "Swapen.com";
		$mail->Subject    = $rowEmail['email_subject'];
		$mail->AddAddress($email, $first_name);
		$mail->Send();
	 if($user_type==3)
	 {
	
	 $msg="<font face=verdana size=2 color='#005B00'><b>There is a 1-3 business day wait for verification purposes, but you can start creating your inventory list today! Once you are verified as a Swapen Merchant, your inventory list will go online!</b></font>";
	
	 }
	 else if($user_type==5)
	 {
	 $msg="<font face=verdana size=2 color='#005B00'><b>There is a 1-3 business day wait for verification purposes, but you can start creating your inventory list today! Once you are verified as a Swapen Wholeseller, your inventory list will go online!</b></font>";
	 }
	 else
	 {
	 $msg="<font face=verdana size=2 color='#005B00'><b>Your account successfully confirmed, <a href='http://swapen.com/index.php?signin'>Signin here</a> to continue.</b></font>";
	 }
	}
}
else
    $msg = "<font face=verdana size=2 color='#FF0000'><b>This email id already exist in another account, enter valid one.</b></font>"; 
}
else
	$msg = "<font face=verdana size=2 color='#FF0000'><b>Invalid confirmation code.</b></font>";
}
else
	$msg = "<font face=verdana size=2 color='#FF0000'><b>Invalid URL.</b></font>";
	*/
 
?>
<div id='panel'>
<table width="100%" border="0">
  <tr>
    <td class="contentHeader"><h2>Registration confirmation</h2></td>
  </tr>
  <tr>
    <td class="content"><p><?php if(!empty($msg))
		     echo $msg;
	?></p>
    </td>
  </tr>
</table>
</div>